package com.cyl.h5.controller;

import com.cyl.h5.domain.vo.ProductDetailVO;
import com.cyl.h5.domain.vo.RecentlyBuyCVo;
import com.cyl.h5.domain.vo.RecentlyBuyVo;
import com.cyl.manager.pms.convert.ProductConvert;
import com.cyl.manager.pms.domain.entity.Brand;
import com.cyl.manager.pms.domain.entity.Product;
import com.cyl.manager.pms.domain.query.ProductQuery;
import com.cyl.manager.pms.service.BrandService;
import com.cyl.manager.pms.service.ProductService;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品查询接口")
@RestController
@RequestMapping("/app/product")
public class H5ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductConvert productConvert;
    @Autowired
    private BrandService brandService;

    @ApiOperation("获取品牌列表")
    @GetMapping("/brandList")
    public R<List<Brand>> getBrand() {
        try{
            List<Brand> brands = brandService.selectByEntity(new Brand());
            return R.ok(brands);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("商品列表查询")
    @PostMapping("/list")
    public R<TableDataInfo> queryGoodByPage(ProductQuery query, Pageable page) {
       try{
           List<Product> pageRes = productService.selectList(query, page);
           TableDataInfo tableDataInfo = new TableDataInfo();
           tableDataInfo.setTotal(new PageInfo<>(pageRes).getTotal());
           tableDataInfo.setRows(pageRes);
           return R.ok(tableDataInfo);
       }catch (Exception e){
           return R.fail(e.getMessage());
       }
    }

    @ApiOperation("商品细节查询")
    @GetMapping("/detail/{id}")
    public R<ProductDetailVO> queryDetail(@PathVariable Long id) {
        try {
            ProductDetailVO detail = productService.queryDetail(id);
            return R.ok(detail);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("首页轮播图")
    @GetMapping("/indexBanner")
    public R<List<Product>> queryIndexBanner() {
        try {
            List<Product> indexBanner = productService.selectIndexBanner();
            return R.ok(indexBanner);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("最近购买")
    @GetMapping("/recently")
    public R<RecentlyBuyVo> queryRecentlyBuy(Long productId) {
        try {
            RecentlyBuyVo recentlyBuy = productService.queryRecentlyBuy(productId);
            return R.ok(recentlyBuy);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }


}
